package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.ToggleButton;
import cn.o.app.ui.OAlert;
import cn.o.app.ui.OAlert.OAlertListener;
import cn.o.app.ui.OImageView;
import cn.o.app.ui.photo.PhotoActivity;
import cn.o.app.ui.photo.PhotoExtra;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.evan.common.connection.ConnectionCommon;
import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.utils.SemicolonTokenizer;
import com.evan.common.widget.ItemView;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.vo.TwoBeanVo;

/**
 * @{# ZhiliaofanganActivity_V1_1.java Create on 2014-10-15 上午09:17:28
 * 
 * @author <a href="mailto:evan0502@qq.com">Evan</a>
 * @version 1.0
 */

@SuppressLint("InflateParams")
public class ZhiliaofanganActivity extends BasicActivity {
	// 缓存上次解答输入
	public static HashMap<String, ZhiliaofanganDraft> sDrafts = new HashMap<String, ZhiliaofanganActivity.ZhiliaofanganDraft>();

	public static class ZhiliaofanganDraft {
		public String cache_bingzheng_gaishu = "";
		public String cache_yongyao = "";
		public String cache_yizhu = "";
		public String cache_questionCategory = "";
		public String cache_bingzhengtuiduan = "";
	}

	TextView text_question_name;

	TextView text_jieda_tip;

	TextView text_user_info;

	TextView txt_tijiao_jieda;

	MultiAutoCompleteTextView edit_yongyao;

	ToggleButton checkBtn;

	EditText edit_patient_zhengzhunag_gaishu;

	EditText edit_bingzhengtuiduan;

	EditText edit_yizhu;

	LinearLayout ll_cuoshi;

	LinearLayout llImg;

	TextView textDoctorTip;

	ItemView itemviewQuestionCategory;

	// 临时变量
	TextView preYongyao;

	JSONObject question, answer, root, patient;

	private ArrayList<View> cuoshiView;

	private String qid;
	ArrayAdapter<String> adapter;
	private boolean isBingli;
	private LinearLayout mLlCuoshi;
	private ArrayList<String> mMedicinal = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhiliao_fangan);

		text_question_name = (TextView) findViewById(R.id.text_question_name);
		text_user_info = (TextView) findViewById(R.id.text_user_info);
		text_jieda_tip = findViewById(R.id.txt_jiedayijian, TextView.class);
		txt_tijiao_jieda = findViewById(R.id.txt_tijiao_jieda, TextView.class);
		edit_yongyao = (MultiAutoCompleteTextView) findViewById(R.id.edit_yongyao);
		edit_patient_zhengzhunag_gaishu = (EditText) findViewById(R.id.edit_patient_zhengzhunag_gaishu);
		edit_bingzhengtuiduan = (EditText) findViewById(R.id.edit_bingzhengtuiduan);
		edit_yizhu = (EditText) findViewById(R.id.edit_yizhu);
		ll_cuoshi = (LinearLayout) findViewById(R.id.ll_cuoshi);
		llImg = (LinearLayout) findViewById(R.id.ll_imgs);
		textDoctorTip = (TextView) findViewById(R.id.text_doctor_tip);
		itemviewQuestionCategory = (ItemView) findViewById(R.id.itemview_question_category);
		checkBtn = findViewById(R.id.shi_fou_gong_kai, ToggleButton.class);
		mLlCuoshi = (LinearLayout) findViewById(R.id.ll_cuoshi);
		// txt_tijiao_jieda.setOnClickListener(this);

		init();
		setNavTitle("创建解答意见");
		// setNavRightBtn("提交解答");
		String str = CommonUtility
				.getStringToBean(new Object(), "Medicinal.txt", getContext())
				.replace("[", "").replace("]", "");
		String[] array = str.split(",");
		for (String tmp : array) {
			mMedicinal.add(tmp.replace("\"", ""));
		}

		adapter = new ArrayAdapter<String>(this, R.layout.item_filter,
				mMedicinal);
		edit_yongyao.setAdapter(adapter);
		edit_yongyao.setThreshold(1);
		edit_yongyao.setTokenizer(new SemicolonTokenizer(' '));
		String jiedaTip = "解答意见(" + "<font color='#c5506d'>必填</font>" + ")";
		text_jieda_tip.setText(Html.fromHtml(jiedaTip));

		isBingli = getIntent().getBooleanExtra(Constant.IDENTITY_KEY_BINGLI,
				false);

		// 初始化数据
		try {
			root = new JSONObject(getIntent().getStringExtra(
					Constant.IDENTITY_KEY));
			question = root.getJSONObject("Question");
			qid = question.getString("Qid");
			patient = root.getJSONObject("Patient_User");
			answer = root.getJSONObject("Answer");

			text_question_name.setText(question.getString("Content"));
			// 用户信息
			StringBuilder sb = new StringBuilder();
			String sex = question.getInt("Sex") == 0 ? "男" : "女";
			sb.append(sex).append("，").append(question.getInt("Age"))
					.append("岁");

			String nation = patient.getString("Nation");
			if (!CommonUtility.isNull(nation)) {
				// sb.append("，").append(nation);
			}
			String maritalStatus = patient.getInt("Marital_Status") == 0 ? "未婚"
					: "已婚";
			// sb.append("，").append(maritalStatus);

			StringBuilder otherInfoBuilder = new StringBuilder();
			String bingshi = CommonUtility.convertJSONArray2String(
					question.getJSONArray("MedicalHistory"), "、");
			String guoMinShi = CommonUtility.convertJSONArray2String(
					question.getJSONArray("DrugAllergen"), "、");
			String puTongYaoWuGuoMin = CommonUtility.convertJSONArray2String(
					question.getJSONArray("Allergen"), "、");
			String jiaren = CommonUtility.convertJSONArray2String(
					question.getJSONArray("FamilyMedicalHistory"), "、");

			if (!CommonUtility.isNull(bingshi)) {
				otherInfoBuilder.append(bingshi).append("、");
			}
			if (!CommonUtility.isNull(guoMinShi)) {
				otherInfoBuilder.append(guoMinShi).append("、");
			}
			if (!CommonUtility.isNull(puTongYaoWuGuoMin)) {
				otherInfoBuilder.append(puTongYaoWuGuoMin).append("、");
			}
			if (!CommonUtility.isNull(jiaren)) {
				otherInfoBuilder.append(jiaren);
			}
			if (!CommonUtility.isNull(otherInfoBuilder)) {
				sb.append("，").append("对").append(otherInfoBuilder);
			}
			text_user_info.setText(sb);

			// 图片显示
			JSONArray pics = question.getJSONArray("Pic");
			llImg.removeAllViews();
			if (pics.length() > 0) {
				String[] picStrs = new String[pics.length()];
				for (int i = 0; i < pics.length(); i++) {
					OImageView layout = new OImageView(activity);
					layout.setIsRoundRect(true);
					layout.setId(R.id.imgLayout_show);
					layout.setScaleType(ScaleType.CENTER_CROP);
					LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
							(int) OUtil.dp2px(activity, 70), (int) OUtil.dp2px(
									activity, 70));
					layoutParams.setMargins(0, 0,
							(int) OUtil.dp2px(activity, 8), 0);
					layout.setLayoutParams(layoutParams);

					final String path = pics.getString(i) + Constant.IMG_TH;
					mBitmapUtils.display(layout, GlobalSettings.SERVER_IMG_URL
							+ path);
					llImg.addView(layout);
					layout.setOnClickListener(listener);
					layout.setTag(i);
					layout.setTag(R.id.tag_obj, picStrs);
					picStrs[i] = ConnectionCommon.getInstance().REQUEST_PIC_PATH
							+ pics.getString(i) + Constant.IMG_SRC;
				}
			}

			// 显示友好提示
			String content = "<font color='#777777'>请为您的患者创建一份解答意见，帮助他改善病情。填写完成后点击右上角</font><font color='#49d29d'>提交解答</font><font color='#777777'>。</font>";
			textDoctorTip.setText(Html.fromHtml(content));

			edit_patient_zhengzhunag_gaishu.setText(answer.getString("Malady"));
			itemviewQuestionCategory.setTextValueValue(CommonUtility
					.convertJSONArray2String(answer.getJSONArray("Symptom"),
							"、"));

			if (!answer.getString("Disease_Inference").isEmpty())
				;
			edit_bingzhengtuiduan
					.setText(answer.getString("Disease_Inference"));

			JSONArray treatments = answer.getJSONArray("Treatments");
			int length = treatments.length();
			if (!CommonUtility.isNull(treatments) && length > 0) {
				JSONObject treatment = treatments.getJSONObject(0);
				edit_yongyao.setText(treatment.getString("Drug"));
				edit_yizhu.setText(treatment.getString("Advice"));
				for (int i = 1; i < length; i++) {
					treatment = treatments.getJSONObject(i);
					if (CommonUtility.isNull(cuoshiView)) {
						cuoshiView = new ArrayList<View>();
					}
					View v = LayoutInflater.from(activity).inflate(
							R.layout.item_cuoshi, null);
					int count = ll_cuoshi.getChildCount();
					ll_cuoshi.addView(v, count);
					TextView text_cuoshi_num = (TextView) v
							.findViewById(R.id.text_cuoshi_num);
					count++;
					text_cuoshi_num.setText("措施" + count);
					MultiAutoCompleteTextView edit_yongyao = (MultiAutoCompleteTextView) v
							.findViewById(R.id.edit_yongyao);
					edit_yongyao.setAdapter(adapter);
					edit_yongyao.setThreshold(1);
					edit_yongyao.setTokenizer(new SemicolonTokenizer(' '));
					EditText edit_yizhu = (EditText) v
							.findViewById(R.id.edit_yizhu);
					Button btnDel = (Button) v.findViewById(R.id.btn_delete);
					btnDel.setTag(v);
					edit_yongyao.setText(treatment.getString("Drug"));
					edit_yizhu.setText(treatment.getString("Advice"));
					cuoshiView.add(v);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		edit_patient_zhengzhunag_gaishu
				.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						EditText textView = (EditText) v;
						String hint;
						if (hasFocus) {
							hint = textView.getHint().toString();
							textView.setTag(hint);
							textView.setHint(null);
						} else {
							hint = textView.getTag().toString();
							textView.setHint(hint);
						}
					}
				});

		CommonUtility.showKeyboard(edit_patient_zhengzhunag_gaishu);

		ZhiliaofanganDraft draft = sDrafts.get(qid);
		if (draft == null) {
			draft = new ZhiliaofanganDraft();
			sDrafts.put(qid, draft);
		}
		if (!isBingli) {
			edit_patient_zhengzhunag_gaishu
					.setText(draft.cache_bingzheng_gaishu);
			edit_patient_zhengzhunag_gaishu
					.addTextChangedListener(new TextWatcher() {

						@Override
						public void onTextChanged(CharSequence s, int start,
								int before, int count) {

						}

						@Override
						public void beforeTextChanged(CharSequence s,
								int start, int count, int after) {

						}

						@Override
						public void afterTextChanged(Editable s) {
							ZhiliaofanganDraft draft = sDrafts.get(qid);
							if (draft == null) {
								draft = new ZhiliaofanganDraft();
								sDrafts.put(qid, draft);
							}
							draft.cache_bingzheng_gaishu = s.toString();
						}
					});
			edit_yongyao.setText(draft.cache_yongyao);
			edit_yongyao.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					ZhiliaofanganDraft draft = sDrafts.get(qid);
					if (draft == null) {
						draft = new ZhiliaofanganDraft();
						sDrafts.put(qid, draft);
					}
					draft.cache_yongyao = s.toString();
				}
			});
			edit_yizhu.setText(draft.cache_yizhu);
			edit_yizhu.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					ZhiliaofanganDraft draft = sDrafts.get(qid);
					if (draft == null) {
						draft = new ZhiliaofanganDraft();
						sDrafts.put(qid, draft);
					}
					draft.cache_yizhu = s.toString();
				}
			});
			itemviewQuestionCategory
					.setTextValueValue(draft.cache_questionCategory);
			itemviewQuestionCategory.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					ZhiliaofanganDraft draft = sDrafts.get(qid);
					if (draft == null) {
						draft = new ZhiliaofanganDraft();
						sDrafts.put(qid, draft);
					}
					draft.cache_questionCategory = s.toString();
				}
			});

			if (!draft.cache_bingzheng_gaishu.isEmpty())
				edit_bingzhengtuiduan.setText(draft.cache_bingzhengtuiduan);
			edit_bingzhengtuiduan.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					ZhiliaofanganDraft draft = sDrafts.get(qid);
					if (draft == null) {
						draft = new ZhiliaofanganDraft();
						sDrafts.put(qid, draft);
					}
					draft.cache_bingzhengtuiduan = s.toString();
				}
			});
		}
	}

	@Override
	public void onClick(final View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.itemview_question_category) {
			startActivityForResult(new Intent(activity,
					ZhengzhuangActivity.class), 0);
		} else if (id == R.id.itemview_choose_template) {
			Intent intent = new Intent(activity, ChooseTemplateActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY, View.VISIBLE);
			startActivityForResult(intent, 0);
		} else if (id == R.id.btn_save_zhiliaofangan) {
			String bingzhengGaishu = CommonUtility
					.getText(edit_patient_zhengzhunag_gaishu);
			String yongyao = CommonUtility.getText(edit_yongyao);
			String yizhu = CommonUtility.getText(edit_yizhu);
			String questionCategory = itemviewQuestionCategory
					.getTextValueValue();
			String bingZhengTuiDuan = CommonUtility
					.getText(edit_bingzhengtuiduan);
			Intent intent = new Intent(activity,
					CreateZhiliaofanganActivity.class);
			String[] datas = { bingzhengGaishu, yongyao, yizhu,
					questionCategory, bingZhengTuiDuan };
			// 获取新增措施数据
			if (!CommonUtility.isNull(cuoshiView)) {
				int cuoshiSize = cuoshiView.size();
				String[][] cuoshiDatas = new String[cuoshiSize][];
				for (int i = 0; i < cuoshiSize; i++) {
					View cuoshi = cuoshiView.get(i);
					MultiAutoCompleteTextView edit_yongyao = (MultiAutoCompleteTextView) cuoshi
							.findViewById(R.id.edit_yongyao);
					edit_yongyao.setAdapter(adapter);
					edit_yongyao.setThreshold(1);
					edit_yongyao.setTokenizer(new SemicolonTokenizer(' '));
					EditText edit_yizhu = (EditText) cuoshi
							.findViewById(R.id.edit_yizhu);
					cuoshiDatas[i] = new String[] {
							CommonUtility.getText(edit_yongyao),
							CommonUtility.getText(edit_yizhu) };
				}
				TwoBeanVo vo = new TwoBeanVo();
				vo.datas = cuoshiDatas;
				intent.putExtra(Constant.IDENTITY_KEY_1, vo);
			}
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			startActivity(intent);
		} else if (id == R.id.txt_tijiao_jieda) {
			try {
				String bingzheng_gaishu = edit_patient_zhengzhunag_gaishu
						.getText().toString();
				if (CommonUtility.isNull(bingzheng_gaishu)) {
					CommonUtility.tip(activity, "请输入解答意见");
					return;
				}
				String yongyao = CommonUtility.getText(edit_yongyao);
				String yizhu = CommonUtility.getText(edit_yizhu);
				// if (CommonUtility.isNull(yizhu)) {
				// CommonUtility.tip(activity, "请输入医嘱");
				// return;
				// }

				String questionCategory = itemviewQuestionCategory
						.getTextValueValue();
				// if (CommonUtility.isNull(questionCategory)) {
				// CommonUtility.tip(activity, "请选择问题分类");
				// return;
				// }

				String bingZhengTuiDuan = CommonUtility
						.getText(edit_bingzhengtuiduan);
				HashMap<String, Object> params = new HashMap<String, Object>();
				params.put("Malady", bingzheng_gaishu);
				params.put("Disease_Inference", bingZhengTuiDuan);
				params.put("Symptom", questionCategory);
				params.put("Drug_0", yongyao);
				params.put("Advice_0", yizhu);
				// 将最新的值注入jsonobject 用于通知前一个页面局部刷新
				answer.put("Malady", bingzheng_gaishu);
				answer.put("Disease_Inference", bingZhengTuiDuan);
				answer.put("Symptom", questionCategory);

				JSONArray treatments = new JSONArray();
				JSONObject treatment = new JSONObject();
				treatment.put("Drug", yongyao);
				treatment.put("Advice", yizhu);
				treatments.put(treatment);

				if (isBingli) {
					params.put(Constant.PARAM_API, "MR.Question.Set");
				} else {
					params.put(Constant.PARAM_API, "Answer.Set");
				}
				if (checkBtn.isChecked()) {
					params.put("Is_Hide_Answer", 0);
				} else {
					params.put("Is_Hide_Answer", 1);
				}
				params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
				try {
					qid = question.getString("Qid");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				if (!CommonUtility.isNull(qid)) {
					if (isBingli) {
						params.put("qId", qid);
						try {
							params.put("mruid", question.getString("Uid"));
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else {
						params.put("Qid", qid);
					}
				}
				if (!CommonUtility.isNull(cuoshiView)) {
					int cuoshiSize = cuoshiView.size();
					params.put("TreatMentsCount", cuoshiSize + 1);
					for (int i = 0; i < cuoshiSize; i++) {
						View cuoshi = cuoshiView.get(i);
						MultiAutoCompleteTextView edit_yongyao = (MultiAutoCompleteTextView) cuoshi
								.findViewById(R.id.edit_yongyao);
						edit_yongyao.setAdapter(adapter);
						edit_yongyao.setThreshold(1);
						edit_yongyao.setTokenizer(new SemicolonTokenizer(' '));
						EditText edit_yizhu = (EditText) cuoshi
								.findViewById(R.id.edit_yizhu);
						params.put("Drug_" + (i + 1),
								CommonUtility.getText(edit_yongyao));
						params.put("Advice_" + (i + 1),
								CommonUtility.getText(edit_yizhu));
						treatment = new JSONObject();
						treatment
								.put("Drug", edit_yongyao.getText().toString());
						treatment
								.put("Advice", edit_yizhu.getText().toString());
						treatments.put(treatment);
					}
				} else {
					params.put("TreatMentsCount", 1);
				}
				answer.put("Treatments", treatments);
				RequestTaskIntercept.requestIntercept(activity,
						Constant.URL.REQUEST, new RequestHandler(activity,
								new HandlerCallback() {

									@Override
									public void callback(Object o) {
										if (!CommonUtility.isNull(o)) {
											JSONObject object = (JSONObject) o;
											if (CommonUtility
													.response200(object)) {
												CommonUtility.toast(activity,
														"生成治疗方案成功");
												if (!isBingli) {
													HashMap<String, Object> p = new HashMap<String, Object>();
													p.put(Constant.PARAM_API,
															"Question.Set");
													p.put("Status", 3);
													p.put("qid", qid);
													p.put(Constant.PARAM_TOKEN,
															GlobalSettings.sToken);
													RequestTaskIntercept
															.requestIntercept(
																	activity,
																	Constant.URL.REQUEST,
																	null,
																	false, p);
												}
												Intent data = new Intent(
														Constant.RECIVER_COMMENT);
												try {
													question.put("Status", 3);
													root.put("Answer", answer);
													root.put("Question",
															question);
												} catch (Exception e) {
													e.printStackTrace();
												}
												data.putExtra(
														Constant.IDENTITY_KEY,
														root.toString());
												sendBroadcast(data);

												data = new Intent(
														Constant.RECIVER_CHAT_FINISH_REFRESH);
												data.putExtra(
														Constant.IDENTITY_KEY,
														root.toString());
												sendBroadcast(data);
												CommonUtility
														.finishActivityFromName(ChatActivity.class
																.getSimpleName());
												finish();
											} else {
												try {
													CommonUtility.toast(
															activity,
															object.getString(Constant.JSON_PARAM_RESULTMESSAGE));

												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										}
									}
								}), params);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (id == R.id.btn_delete) {
			OAlert alert = new OAlert(this);
			alert.setOK("确认");
			alert.setCancel("取消");
			alert.setTitle("确认删除这个措施吗？");
			alert.show();
			alert.setListener(new OAlertListener() {

				@Override
				public boolean onOK(OAlert alert) {
					View child = (View) view.getTag();
					ll_cuoshi.removeView(child);
					cuoshiView.remove(child);
					return false;
				}

				@Override
				public boolean onCancel(OAlert alert) {
					return false;
				}
			});
		} else if (id == R.id.ll_add_cuoshi) {
			createCuoshi();
		}
	}

	private View createCuoshi() {
		if (CommonUtility.isNull(cuoshiView)) {
			cuoshiView = new ArrayList<View>();
		}
		View v = LayoutInflater.from(activity).inflate(R.layout.item_cuoshi,
				null);

		MultiAutoCompleteTextView mYao = (MultiAutoCompleteTextView) v
				.findViewById(R.id.edit_yongyao);
		mYao.setAdapter(adapter);
		mYao.setThreshold(1);
		mYao.setTokenizer(new SemicolonTokenizer(' '));
		int count = ll_cuoshi.getChildCount();
		ll_cuoshi.addView(v, count);
		TextView text_cuoshi_num = (TextView) v
				.findViewById(R.id.text_cuoshi_num);
		count++;
		text_cuoshi_num.setText("措施" + (count + 1));
		Button btnDel = (Button) v.findViewById(R.id.btn_delete);
		btnDel.setTag(v);
		cuoshiView.add(v);
		return v;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (!CommonUtility.isNull(data)) {
			String value = data.getStringExtra(Constant.IDENTITY_KEY);
			switch (resultCode) {
			case Constant.ACTIVITY_CHOOSE_ZHENGZHUANG:
				itemviewQuestionCategory.setTextValueValue(value);
				break;
			case Constant.ACTIVITY_CHOOSE_TEMPLATE:
				try {
					JSONObject object = new JSONObject(value);
					edit_patient_zhengzhunag_gaishu.setText(object
							.getString("Malady"));
					itemviewQuestionCategory.setTextValueValue(CommonUtility
							.convertJSONArray2String(
									object.getJSONArray("Symptom"), ""));
					edit_bingzhengtuiduan.setText(object
							.getString("Disease_Inference"));
					JSONArray treatments = object.getJSONArray("Treatments");
					JSONObject obj = null;
					if (treatments.length() > 0) {
						obj = treatments.getJSONObject(0);
						edit_yongyao.setText(obj.getString("Drug"));
						edit_yizhu.setText(obj.getString("Advice"));
					}

					if (!CommonUtility.isNull(ll_cuoshi)) {
						ll_cuoshi.removeAllViews();
					}
					if (!CommonUtility.isNull(cuoshiView)) {
						cuoshiView.clear();
					}
					for (int i = 1; i < treatments.length(); i++) {
						obj = treatments.getJSONObject(i);
						View cuoshi = createCuoshi();
						MultiAutoCompleteTextView edit_yongyao = (MultiAutoCompleteTextView) cuoshi
								.findViewById(R.id.edit_yongyao);
						EditText edit_yizhu = (EditText) cuoshi
								.findViewById(R.id.edit_yizhu);
						edit_yongyao.setText(obj.getString("Drug"));
						edit_yizhu.setText(obj.getString("Advice"));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}
		}
	}

	// 患处图片点击事件
	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			int position = (Integer) view.getTag();
			String[] pics = (String[]) view.getTag(R.id.tag_obj);
			PhotoExtra extra = new PhotoExtra();
			extra.setDisplayedIndex(position);
			extra.setPhotos(OUtil.asArrayList(pics));
			Intent intent = new Intent(activity, PhotoActivity.class);
			extra.putTo(intent);
			activity.startActivity(intent);
		}
	};

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
